﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MyTime
{
    public partial class Summary : Form
    {
        private const int winWidth = 200;
        private const int maxWidth = 1500;
        public Summary(List<ProviderFactory.Entities.TimeItem> timeItems)
        {
            InitializeComponent();
            this.Width = winWidth;
            //var days = (from item in timeItems orderby item.Date descending select item).Distinct().Take(30);
            var weeks = timeItems.Where(item => item.Date.Date >= DateTime.Now.AddDays(-(4 * 7))).OrderByDescending(d => d.Date).GroupBy(item => item.Week);
            DataGrid grid = null;
            foreach (var week in weeks)
            {
                var daysInWeek = week.Select(day => day).GroupBy(d => d.Day).Select(d => d.First()).OrderByDescending(d => d.Date);
                var title = string.Format("Vecka {0} ({1} - {2})", week.Key, daysInWeek.Min(d => d.Date).ToString("dd MMM"), daysInWeek.Max(d => d.Date).ToString("dd MMM"));
                MyTime.Gui.Controls.WeekGrid wGrid = new Gui.Controls.WeekGrid(title);
                foreach (var day in daysInWeek)
                {
                    var dayTime = timeItems.Where(item => item.Date.Date == day.Date.Date)
                                    .OrderByDescending(i => i.Date)
                                    .GroupBy(i => i.Hour)
                                    .Select(i => i.First())
                                    .Select(i => new { Hour = i.Hour, Message = i.Message })
                                    .OrderBy(i => i.Hour)
                                    .ToList();
                    grid = new DataGrid();
                    grid.CaptionText = string.Format("{0}", day.Date.ToShortDateString());
                    grid.Width = winWidth;
                    grid.Height = this.Height - 60;
                    grid.DataSource = dayTime;
                    wGrid.Append(grid);
                    if (wGrid.Width < maxWidth)
                    {
                        this.Width += winWidth;
                        wGrid.Width += winWidth;
                    }
                }
                flowLayoutPanel1.Controls.Add(wGrid);
            }
        }
    }
}